<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/8/29 0029
 * Time: 上午 9:11
 */
namespace app\admin\controller;

use think\Controller;
use app\common\model\Notice;
use think\Request;

/*
 * 公告管理
 */
class Announcement extends Base {
    public $noticeDao;
    public function __construct()
    {
        parent::__construct();
        $this->noticeDao =  new Notice();
    }

    /*
     * 公告列表页
     */
    public function index(){
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['title'] = ['like',"%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('pagesize');// 获取总条数
        $count = $this->noticeDao->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $lists = $this->noticeDao->where($map)->page($Nowpage, $limits)->order('update_time desc')->select();
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        if(input('get.page')){
            return json($lists);
        }
        $this->assign('vals', $key);
        $this->view->alert = parseAlert();
        return $this->fetch();
    }

    //添加公告
    public function add(){
        if(request()->isPost()){
            $title = trim(input('title'));
            if ($title == '' || $title == null) {
                alert('error',lang('TITLE CAN NOT NULL'),$_SERVER['HTTP_REFERER']);
            }
            $d_announcement = $this->noticeDao;
            $data = input('');
            unset($data['submit']);
            $data['role_id'] = session('admin_userusername');
            $data['create_time'] = time();
            $data['update_time'] = time();
            if($d_announcement->add($data)) {
                alert('success', lang('NOTICE TO ADD SUCCESS'), url('announcement/index'));
            }else{
                $this->error($d_announcement->getError());
            }
        }else{
            $this->view->alert = parseAlert();
            return $this->fetch();
        }
    }


    /*
     * 删除公告
     */
    public function delete(){
        $m_announcement = $this->noticeDao;
        $announcement_idarray = $_POST['announcement_id'];
        if (is_array($announcement_idarray)) {
            $ret = $m_announcement->where('`announcement_id` in ('.join(',', $announcement_idarray).' )')->delete();
            if ($ret) {
                alert('success', lang('DELETED_SUCCESSFULLY'),url('Announcement/index'));
            } else {
                $this->error(lang('DELETED_FAILED'));
            }
        } elseif($_GET['id']) {
            if($m_announcement->where('announcement_id = %d', $_GET['id'])->delete()){
                alert('success', lang('DELETED_SUCCESSFULLY'),url('Announcement/index'));
            }else{
                $this->error(lang('DELETED_FAILED'));
            }
        } else {
            alert('error', L('DELETED_FAILED'),$_SERVER['HTTP_REFERER']);
        }
    }

    //查看
    public function check(){
        $announcement_id = input('id');
        $noticeinfo = $this->noticeDao->search($announcement_id);
        $this->view->data = $noticeinfo;
        $this->view->alert = parseAlert();
        return $this->fetch('add');
    }

    //编辑
    public function edit(){
        $announcement_id = input('announcement_id');
        if(request()->isPost()){
            $title = trim(input('title'));
            if ($title == '' || $title == null) {
                alert('error',lang('TITLE_CAN_NOT_NULL'),$_SERVER['HTTP_REFERER']);
            }
            $m_announcement = $this->noticeDao;
            if($announcement_id){
                $data['title'] = $title;
                $data['role_id'] = session('admin_userusername');
                $data['content'] = input('content');
                $data['update_time'] = time();
                $data['status'] = input('status');
                if($m_announcement->where(array('announcement_id'=>$announcement_id))->update($data)){
                    alert('success', lang('ANNOUNCEMENT SAVED SUCCESSFULLY'), url('announcement/index'));
                } else {

                    $this->error($m_announcement->getError());
                }
            }else{
                alert('error', lang('PARAMETER_ERROR'), url('announcement/index'));
            }
        }else if($announcement_id){
            $m_announcement = $this->noticeDao;
            $announcement = $m_announcement->search($announcement_id);
            $this->view->data = $announcement;
            $this->view->alert = parseAlert();
            return $this->fetch('add');
        }else{
            $this->error(lang('PARAMETER_ERROR'));
        }
    }


    //停用状态
    public function changeStatus(){
        $m_announcement = $this->noticeDao;
        $announcement_id = intval(input('id'));
        if ($announcement_id) {
            $announcement = $m_announcement->search($announcement_id);
            if ($announcement->status == 1) {
                $m_announcement->mod($announcement_id,array('status'=>2));
                alert('success',lang('MODIFY_SUCCESSFULLY'),$_SERVER['HTTP_REFERER']);
            } elseif($announcement['status'] == 2) {
                $m_announcement->mod($announcement_id,array('status'=>1));
                alert('success',lang('MODIFY_SUCCESSFULLY'),$_SERVER['HTTP_REFERER']);
            } else {
                alert('success',lang('SYSTEM_ERROR_PLEASE_CONTACT_YOUR_ADMINISTRATOR'),$_SERVER['HTTP_REFERER']);
            }
        }else{
            alert('error',lang('PARAMETER_ERROR'),$_SERVER['HTTP_REFERER']);
        }
    }

    //停用状态
    public function status(){
        $m_announcement = $this->noticeDao;
        $announcement_id = intval(input('id'));
        if ($announcement_id) {
            $announcement = $m_announcement->search($announcement_id);
            if ($announcement->status == 1) {
                $m_announcement->mod($announcement_id,array('status'=>2));
                return json(['code' => 2, 'data' => '', 'msg' => lang('MODIFY_SUCCESSFULLY')]);
            } elseif($announcement['status'] == 2) {
                $m_announcement->mod($announcement_id,array('status'=>1));
                return json(['code' => 1, 'data' => '', 'msg' => lang('MODIFY_SUCCESSFULLY')]);
            } else {
                return json(['code' => 2, 'data' => '', 'msg' => lang('SYSTEM_ERROR_PLEASE_CONTACT_YOUR_ADMINISTRATOR')]);
            }
        }else{
            return json(['code' => 2, 'data' => '', 'msg' => lang('PARAMETER_ERROR')]);
        }
    }
}